package com.guohang.mic_01;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

//handles database access from application on top of sqlitehelper class
public class CommentsDataSource {

  // Database fields
  private SQLiteDatabase database;
  private MySQLiteHelper dbHelper;
  private String[] allColumns = { MySQLiteHelper.COLUMN_ID, MySQLiteHelper.COLUMN_CLUSTER, MySQLiteHelper.COLUMN_COUNT, 
		  MySQLiteHelper.COLUMN_LASTUPDATE };

  public CommentsDataSource(Context context) {
    dbHelper = new MySQLiteHelper(context);
  }

  public void open() throws SQLException {
    database = dbHelper.getWritableDatabase();
  }

  public void close() {
    dbHelper.close();
  }

 
  
  public int updateCluster(int cluster){
	  int count = 0;
	  
	  //update database table with new cluster
	  //check if cluster exits in database
	  //check last update time to determine if to update database table
	  Cursor cursor = database.query(MySQLiteHelper.TABLE_NAME,
		        allColumns, MySQLiteHelper.COLUMN_CLUSTER + " = " + cluster, null,
		        null, null, null);
	  if(!cursor.moveToFirst()){
		  //it's a new cluster
		  ContentValues values = new ContentValues();
		  values.put(MySQLiteHelper.COLUMN_CLUSTER, cluster);
		  values.put(MySQLiteHelper.COLUMN_COUNT, 1);
		  values.put(MySQLiteHelper.COLUMN_LASTUPDATE, Calendar.getInstance().getTimeInMillis());
		  long insertId = database.insert(MySQLiteHelper.TABLE_NAME, null, values);		
		  return count = 1;
	  }else{
		  if(checkTime(cursor.getLong(3))){
			  //record exists, update database table
			  count = (int) cursor.getLong(2) + 1;
			  
			  ContentValues values = new ContentValues();
			  values.put(MySQLiteHelper.COLUMN_COUNT, count);
			  values.put(MySQLiteHelper.COLUMN_LASTUPDATE, Calendar.getInstance().getTimeInMillis());
			  
			  int rowsUpdated = database.update(MySQLiteHelper.TABLE_NAME, 
			            values,
			            MySQLiteHelper.COLUMN_CLUSTER + "=" + cluster, 
			            null); 
			  cursor.close();
			  return count;
			  
		  }else{
			  cursor.close();
			  return count;
		  }
	  }
  }
  
  private boolean checkTime(long lastTime)
  {
      //long timelow = 2400000;
	  long timelow = 1000;
      long timehigh = 72000000;
	  
	  // how much time has passed since the last update?
      long currTime = Calendar.getInstance().getTimeInMillis();
      long dt = currTime - lastTime;     
      
      if (dt > timelow && dt < timehigh )
	    {
      	return true; 
	    }else{return false;}
  }
  
  /*public Comment createComment(String comment) {
    ContentValues values = new ContentValues();
    values.put(MySQLiteHelper.COLUMN_COMMENT, comment);
    long insertId = database.insert(MySQLiteHelper.TABLE_COMMENTS, null,
        values);
    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
        allColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
        null, null, null);
    cursor.moveToFirst();
    Comment newComment = cursorToComment(cursor);
    cursor.close();
    return newComment;
  }

  public void deleteComment(Comment comment) {
    long id = comment.getId();
    System.out.println("Comment deleted with id: " + id);
    database.delete(MySQLiteHelper.TABLE_COMMENTS, MySQLiteHelper.COLUMN_ID
        + " = " + id, null);
  }

  public List<Comment> getAllComments() {
    List<Comment> comments = new ArrayList<Comment>();

    Cursor cursor = database.query(MySQLiteHelper.TABLE_COMMENTS,
        allColumns, null, null, null, null, null);

    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {
      Comment comment = cursorToComment(cursor);
      comments.add(comment);
      cursor.moveToNext();
    }
    // Make sure to close the cursor
    cursor.close();
    return comments;
  }

  private Comment cursorToComment(Cursor cursor) {
    Comment comment = new Comment();
    comment.setId(cursor.getLong(0));
    comment.setComment(cursor.getString(1));
    return comment;
  } */
} 